<template>
	<view class="container">
		<uni-list v-if="showStatus">
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="猪只耳号"
				:rightText="pigTransferList.pigCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="猪只种类"
						   :rightText="pigType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="转出栏舍类型"
						   :rightText="transferOutPhouseType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="转出栏舍编码"
						   :rightText="pigTransferList.transferOutPhouseCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="转入栏舍类型"
						   :rightText="transferIntoPhouseType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="转入栏舍编码"
						   :rightText="pigTransferList.transferOutPhouseCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'tune-filled'}" title="猪只重量"
						   :rightText="pigTransferList.transferPigWeight+' kg'" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'calendar-filled'}" title="转舍日期"
				:rightText="pigTransferList.transferDate" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'chat-filled'}" title="备注">
				<template v-slot:footer>
					<input type="textarea" v-model="pigTransferList.remark"
						style="font-size: 12px;color: #999999;text-align: right;" />
				</template>
			</uni-list-item>

		</uni-list>
		<view class="button-group" v-if="showStatus">
			<button type="warn" plain="true" @click="pigTransferDelete" style="width: 45%;border: #E53F32 solid 1px;">删 除</button>
			<button type="primary" plain="ture" @click="pigTransferEdit" style="width: 45%;">修 改</button>
		</view>
		<!-- 修改 -->
		<view class="example" v-if="!showStatus">
		  <uni-forms ref="form" :model="pigTransferForm"   :rules="rules" labelWidth="80px">
		    <uni-forms-item label="猪只耳号" name="pigCode" required>
		      <uni-easyinput v-model="pigTransferForm.pigCode" placeholder="请输入猪只耳号" />
		    </uni-forms-item>
			  <uni-forms-item label="猪只类型" name="pigType" required>
				  <uni-data-select v-model="pigTransferForm.pigType" :placeholder="pigType"
								   :localdata="pigTypes"
				  ></uni-data-select>
			  </uni-forms-item>
			  <uni-forms-item label="转出栏舍类型" name="transferOutPhouseType" required>
				  <uni-data-select v-model="pigTransferForm.transferOutPhouseType" :placeholder="transferOutPhouseType"
								   :localdata="transferOutPhouseTypes"
				  ></uni-data-select>
			  </uni-forms-item>
			  <uni-forms-item label="转出栏舍编码" name="transferOutPhouseCode" required>
				  <uni-easyinput v-model="pigTransferForm.transferOutPhouseCode" placeholder="请输入转出栏舍编码" />
			  </uni-forms-item>
			  <uni-forms-item label="转入栏舍类型" name="materiaFeedType" required>
				  <uni-data-select v-model="pigTransferForm.transferIntoPhouseType" :placeholder="transferIntoPhouseType"
								   :localdata="transferIntoPhouseTypes"
				  ></uni-data-select>
			  </uni-forms-item>
			  <uni-forms-item label="转入栏舍编码" name="transferIntoPhouseCode" required>
				  <uni-easyinput v-model="pigTransferForm.transferIntoPhouseCode" placeholder="请输入转入栏舍编码" />
			  </uni-forms-item>
			<uni-forms-item label="转舍时间" name="transferDate" required>
			  <uni-datetime-picker type="datetime" v-model="pigTransferForm.transferDate"/>
			</uni-forms-item>
			<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="pigTransferForm.remark" placeholder="请输入备注" />
				</uni-forms-item>
			  </uni-forms>
			  <view class="edit-button-group">
			  	<button type="primary" @click="back" style="width: 45%;">返 回</button>
			  	<button type="primary" @click="editPigTransfer('form')" style="width: 45%;">提 交</button>
			  </view>
			</view>
	</view>
</template>

<script>
	import {getPigTransfer, updatePigTransfer, delPigTransfer} from "@/api/hmy/pigTransfer";


	export default {
		data() {
			return {
				showStatus:true,
				transferId: '',
				pigCode:'',
				pigType:'',
				pigTransferList:{},
				pigTransferForm:{},
				transferOutPhouseType:'',
				transferIntoPhouseType:'',
				//猪只类型
				pigTypes:[
					{ value: 1, text: '种猪'},
					{ value: 2, text: '母猪'},
					{ value: 3, text: '肉猪'},
				],
				//栏舍类型
				transferOutPhouseTypes:[
					{ value: 1, text: '种猪舍'},
					{ value: 2, text: '母猪舍'},
					{ value: 3, text: '肉猪舍'},
				],
				//栏舍类型
				transferIntoPhouseTypes:[
					{ value: 1, text: '种猪舍'},
					{ value: 2, text: '母猪舍'},
					{ value: 3, text: '肉猪舍'},
				],
				rules: {
					pigCode: {
						rules: [{required: true, errorMessage: '猪只耳号不能为空'}]
					},
					pigType: {
						rules: [{required: true, errorMessage: '猪只类型不可为空'}]
					},
					transferOutPhouseType: {
				  		rules: [{required: true, errorMessage: '转出栏舍类型不能为空'}]
					},
					transferIntoPhouseType: {
						rules: [{required: true, errorMessage: '转入栏舍类型不能为空'}]
					},transferOutPhouseCode: {
						rules: [{required: true, errorMessage: '转出栏舍类型不能为空'}]
					}, transferIntoPhouseCode: {
						rules: [{required: true, errorMessage: '转入栏舍编码不能为空'}]
					}
				}
			}
		},
		onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			//console.log(option.id);
			this.transferId = option.id;

			this.getPigTransferInfo();
		},
		methods: {
			getPigTransferInfo() {
				//显示加载框
				uni.showLoading({
					title:"加载中"
				})
				getPigTransfer(this.transferId).then(response => {
					console.log(3,response)
					this.pigTransferForm = response.data;
					this.pigTransferList = response.data;
				
					// 两个字典类型
					if (this.pigTransferList.pigType == 1) {
						this.pigType = '种猪'
					} else if (this.pigTransferList.pigType == 2) {
						this.pigType = '母猪'
					}else if (this.pigTransferList.pigType == 3) {
						this.pigType = '肉猪'
					}
					// 字典类型  转出栏舍类型
					if (this.pigTransferList.transferOutPhouseType == 1) {
						this.transferOutPhouseType = '种猪舍'
					} else if (this.pigTransferList.transferOutPhouseType == 2) {
						this.transferOutPhouseType = '母猪舍'
					}else if (this.pigTransferList.transferOutPhouseType == 3) {
						this.transferOutPhouseType = '肉猪舍'
					}
					// 字典类型  转入栏舍类型
					if (this.pigTransferList.transferIntoPhouseType == 1) {
						this.transferIntoPhouseType = '种猪舍'
					} else if (this.pigTransferList.transferIntoPhouseType == 2) {
						this.transferIntoPhouseType = '母猪舍'
					}else if (this.pigTransferList.transferIntoPhouseType == 3) {
						this.transferIntoPhouseType = '肉猪舍'
					}
					//隐藏加载框
					uni.hideLoading();
					console.log(response)

				})
			},

			//删除
			pigTransferDelete() {
				console.log("delete", this.pigTransferList.transferId)
				this.$modal.confirm('是否确认删除转舍记录编号为"' +this.pigTransferList.transferId + '"的数据项？').then(() => {
					return delPigTransfer(this.pigTransferList.transferId);
				}).then(response => {
					this.$modal.msgSuccess("删除成功");
					this.$tab.navigateTo('/pages/hmy/pigTransfer/index');
				}).catch(()=>{

				});
			},

			//修改
			pigTransferEdit() {
				console.log("edit", this.pigTransferList.transferId)
				this.showStatus = !this.showStatus
			},
			//返回
			back(){
				this.showStatus = !this.showStatus
				this.getPigTransfer();
			},

			//修改提交
			editPigTransfer(form) {
			  this.$refs[form].validate().then(res => {
			    updatePigTransfer(this.pigTransferForm).then(response => {
			      this.$modal.msgSuccess("修改成功");
				  setTimeout(()=>{
				  	this.$tab.navigateTo('/pages/hmy/pigTransfer/index');
				  },1000)

			    })
			  })
			}
		}
	}
</script>

<style>
	page {
		background-color: #ffffff;
	}

	.button-group {
		display: flex;
		justify-content: space-around;
		width:100%;
		position:fixed;
		bottom:5px;

	}

	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	.edit-button-group{
		display: flex;
		justify-content: space-around;
	}
</style>
